import assert = require('assert');
require("../expressExt/FuncExt")
require("../expressExt/ES6Ext")
import {Config} from "../configExt/Config";
import {SqlBuilder} from "../expressExt/db/SqlBuilder";
import {MongoBuilder} from "../expressExt/db/MongoBuilder";
import {ObjectID} from "mongodb";
import {array, arrayNumber} from "../expressExt/public/sdf";
import {sql} from "../expressExt/db/Isql";
import {MongoTable} from "../expressExt/db/MongoTable";

class ArrC {
    aaa = 1;
    @sql.index()
    bbb = 2;
}

class SqlBu {

    _id = new ObjectID();

    /**
     * 名字
     */
    @sql.index()
    name = "aa";

    /**
     * 值
     */
    @sql.indexDesc()
    val = Date.now();

    @sql.indexRaw({'a.aa': 1, 'a.bb': 1})
    a = {aa: 0, bb: 1}

    @sql.index()
    b = arrayNumber([0, 1, 2, 3]);

    c = array(ArrC, [new ArrC(), new ArrC(), new ArrC()]);

    d = new ArrC();

    e = {};

    static mongo(): MongoBuilder<SqlBu> {
        return new MongoBuilder(SqlBu, Config.mongo);
    }

    static sql() {
        return new SqlBuilder<SqlBu>(SqlBu, Config.mysql);
    }

}


describe('sqlMongo Test', function () {


    it("mongo", async()=> {
        // SqlBu.mongo().createAllIndex();
        // await SqlBu.mongo().insertPromise(new SqlBu());
        // await SqlBu.mongo().where(f=> {
        // }).updatePromise(f=>f.c.pull(f=>f.aaa.gt(0) & f.aaa.le(100)) + f.b.pull(f=>f.in([0, 1])) + f.e.$set("zzzzz", 2));
        //
        // let res = await SqlBu.mongo().where(f=>(f.val.eq(2) | f.name.eq("aaa")) | f.val.gtEq(2) | f._id.eq("5770970a2e82993c1ba4a78f")).toArrayPromise();
        //
        //
        // res = await SqlBu.mongo().where(f=>((f.name.eq("aa")))).order(f=>f.val.asc() + f.name.desc()).toArrayPromise();
        // console.error(JSON.stringify(res))
        //
        // let res2 = await  SqlBu.mongo().where(f=>f._id.eq("5770970a2e82993c1ba4a78f") | f.a.aa.eq(2)).set(f=>f.name.set("dddd")).updatePromise(f=>f.val.inc(-2));
        //
        //
        // console.error(JSON.stringify(await SqlBu.mongo().where(f=>((f.name.eq("aa")))).toOnePromise()))
        // console.error(JSON.stringify(await SqlBu.mongo().where(f=>((f.name.in("aa")))).toOnePromise()))
        // console.error(JSON.stringify(await SqlBu.mongo().where(f=>((f.c.at(0).bbb.gtEq(0))) | f.b.$.eq(0) & f.a.bb.leEq(10)).toOnePromise()))
        //
        //
        // console.error(JSON.stringify(await SqlBu.mongo().unselect(f=>f.a as any + f.b + f._id).toArrayPromise()));
    });

});